package com.bike.controller.api;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.bike.controller.BaseController;
import com.bike.entity.HhBusinessUser;
import com.bike.service.impl.HhCityServiceImpl;
import com.bike.util.Constant;
import com.bike.util.Md5Util;
import com.bike.util.PageData;
import com.bike.util.TimeManager;
import com.topit.show.aliapi.AlyOssFileUpLoadUtils;

 
@Controller
@RequestMapping(value="/apiImage")
public class HhApiImageController extends BaseController {
	
		@Autowired
		private HhCityServiceImpl  hhCityService;
	
	 
		/**
		 * 列表查询(或根据条件)
		 */
		@RequestMapping("upload")
		@ResponseBody
		public PageData list(@RequestParam(required=false,value="file") MultipartFile file,String time,String sign,HttpServletRequest request,Model model){
			
			
			PageData pd=PageData.getPageDataByPage(request);
			PageData result=getSuccessPd();
			HhBusinessUser businessUser=(HhBusinessUser) request.getAttribute("businessUser");
			
			if(!Md5Util.MD5(Constant.PhoneNumMD5Prefix.concat(time)).equals(sign)){
				return getErrorPd("上传失败,请检查参数");
			}
			
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
			Iterator<String>  files= multipartRequest.getFileNames();
			String df= ""; 
			for(;files.hasNext();){
				df+=files.next()+"--";
			}
			Set<String> filenames=multipartRequest.getFileMap().keySet();
			
			for(int i=0;i<filenames.size();i++){
				df+=filenames.iterator().next()+"--"+(multipartRequest.getFileMap().get(filenames.iterator().next())==null);
			}
			
			
			String img="";
			int length=0;
			try {
				if(file!=null){
					length=file.getBytes().length;
				}
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			if(length>0&&length<10000000){
				String path=pd.getString("path");
				if(path.equals("")){
					path="shop";
				}
				String fold=path.concat("/").concat(TimeManager.getInstance().getYMD());
				String fileName=file.getOriginalFilename();
				String name=businessUser.getBusinessId().toString().concat(time).concat(fileName.substring(fileName.lastIndexOf(".")));
				String folder=Constant.filePath.concat(fold);
				 img=fold.concat("/").concat(name);
				File upde=new File(folder);
				if(!upde.exists()){
					upde.mkdirs();
				}
				File upfile=new File(upde,name);
				try {
					file.transferTo(upfile);
					new AlyOssFileUpLoadUtils().fileUpload(img, upfile);
				} catch (IllegalStateException | IOException e) {
					e.printStackTrace();
				}
			}
		 
			result.put("url", img);
			return result;
		}
		 
}
